/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package srac.controleatividades.cgt;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import org.zkoss.zul.Filedownload;
import srac.controleatividades.cdp.Aluno;
import srac.controleatividades.cdp.AtividadeComplementar;

/**
 *
 * @author gmiranda
 */
public class SubmissoesToPdf {

    /**
     * Gera o relatorio de Submissoes do aluno em PDF
     * @param listAtv
     * @param a
     * @throws BadElementException
     * @throws MalformedURLException
     * @throws IOException
     * @throws DocumentException 
     */
    public static void gerarPdf(List<AtividadeComplementar> listAtv, Aluno a) throws BadElementException, MalformedURLException, IOException, DocumentException {
        Document document = new Document();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);



        document.open();


        Font f = new Font();
        f.setSize(18);

        Font f2 = new Font();
        f2.setSize(10);

        document.add(new Paragraph("\r\n"));
        Paragraph p;
        p = new Paragraph("Histórico de Atividades", f);


        p.setAlignment(Element.ALIGN_CENTER);

        document.add(p);

        document.add(new Paragraph("\r\n"));
        document.add(new Paragraph("Nome Aluno:  " + a.getNome(), f2));
              
        
        Date d = new Date();
        
        SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");  
        
        document.add(new Paragraph("Data:  "+ formatador.format(d), f2));

        Comparator c = new Comparator<AtividadeComplementar>(){
            @Override
            public int compare(AtividadeComplementar o1, AtividadeComplementar o2)
            {
                return (int) (o1.getTipoAtividade().getCodigo()-o2.getTipoAtividade().getCodigo());
            }
        };
        
        Collections.sort(listAtv, c);
             
        
        document.add(new Paragraph("\r\n"));

        PdfPTable table = new PdfPTable(new float[]{0.12f, 0.23f ,0.45f, 0.20f});
        table.setHorizontalAlignment(Element.ALIGN_CENTER);


        PdfPCell cell = new PdfPCell(new Paragraph("Código"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("Categoria"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        

        cell = new PdfPCell(new Paragraph("Descrição"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("Créditos Requeridos"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        Integer soma=0;
        for (AtividadeComplementar at : listAtv) {
            cell = new PdfPCell(new Paragraph(at.getTipoAtividade().getCodigo().toString(), f2));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);

            table.addCell(cell);
            
            cell = new PdfPCell(new Paragraph(at.getTipoAtividade().getCategoria().toString(), f2));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            

            cell = new PdfPCell(new Paragraph(at.getDescricao(), f2));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph(at.getCreditosRequeridos().toString(), f2));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            
            soma+= at.getCreditosRequeridos();
        }
        
        document.add(table);
        
        document.add(new Paragraph("\r\n"));
        
        document.add(new Paragraph("Total de créditos:  " + soma.toString() , f2));
        
        document.add(new Paragraph("\r\n"));
        
        document.close();
        Filedownload fd = null;
        fd.save(baos.toByteArray(), null, a.getMatricula()+".pdf");
        
        //TODO mandar o arquivo para cima (e lá pedir para o usuario salvar)
    }
}
